﻿using System.Collections.Generic;

namespace SimpleTaskManager
{
    public class Task
    {
        public User AssignTo;
        public string Description;
        public int Id;
        public Project Project;
        public string Title;

        public Task()
        {
            Id = GetHashCode();
        }


        public static Task Load(int id)
        {
            foreach (Task task in Storage.Tasks)
            {
                if (task.Id == id)
                {
                    return task;
                }
            }
            throw new  MissingEntityException();
        }

        public static List<Task> List(Project project)
        {
            var result = new List<Task>();
            foreach (Task task in Storage.Tasks)
            {
                if (task.Project == project)
                {
                    result.Add(task);
                }
            }
            return result;
        }

        public static List<Task> List(User user)
        {
            var result = new List<Task>();
            foreach (Task task in Storage.Tasks)
            {
                if (task.AssignTo == user)
                {
                    result.Add(task);
                }
            }
            return result;
        }

        public void Save()
        {
            if (!Storage.Tasks.Contains(this))
            {
                Storage.Tasks.Add(this);
            }
        }

        public void Delete()
        {
            Storage.Tasks.Remove(this);
        }

        public override bool Equals(object obj)
        {
            return ((Task)obj).Id == this.Id;
        }
        public static bool operator ==(Task a, Task b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(Task a, Task b)
        {
            return !(a == b);
        }
    }
}